﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.Dal
{
    /// <summary>
    /// EFCore
    /// </summary>
    public partial class DbEntities : DbContext
    {
        #region 构造函数和基础

        public DbEntities()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(SysConfig.DbConnectionString,
                o =>
                {
                    o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
                });
            //SplitQuery是拆分查询,针对db.X.InClude(m=>m.Y).ToList() 或FirstOrDefault这类查询的
            //optionsBuilder.UseSqlServer(SysConfig.DbConnectionString,
            //    o =>
            //    {
            //        o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
            //    });
            //延迟加载
            //Microsoft.EntityFrameworkCore.Proxies.dll
            //optionsBuilder.UseLazyLoadingProxies();
            //要将数据库迁移到的目标迁移，或 null 迁移到最新的
            //Database.Migrate();
            //确保上下文的数据库不存在。 如果不存在，则不执行任何操作。 如果存在，则删除数据库。
            //Database.EnsureDeleted();
            //确保上下文的数据库存在。 如果存在，则不需要任何操作。 如果该数据库不存在，则创建数据库及其所有架构。 如果数据库存在，则不做出任何努力来确保它与此上下文的模型兼容。
            //Database.EnsureCreated();
        }

        #endregion

        #region 数据库表/系统基础数据库表

        public DbSet<SysLog> SysLog { get; set; }

        public DbSet<SysStatisticalSql> SysStatisticalSql { get; set; }

        public DbSet<SysDic> SysDic { get; set; }

        public DbSet<SysStore> SysStore { get; set; }

        public DbSet<SysEmployeeStoreRelation> SysEmployeeStoreRelation { get; set; }

        public DbSet<SysEmployee> SysEmployee { get; set; }

        public DbSet<SysEmployeeOpenID> SysEmployeeOpenID { get; set; }

        public DbSet<SysRolePostEmployeeRelation> SysRolePostEmployeeRelation { get; set; }

        public DbSet<SysOperator> SysOperator { get; set; }

        public DbSet<SysRolePost> SysRolePost { get; set; }

        public DbSet<SysRolePostPowerRelation> SysRolePostPowerRelation { get; set; }

        public DbSet<SysPower> SysPower { get; set; }

        public DbSet<SysSetting> SysSetting { get; set; }

        public DbSet<SysOperateLog> SysOperateLog { get; set; }

        public DbSet<SysAccessTokenInfo> SysAccessTokenInfo { get; set; }

        public DbSet<SysFile> SysFile { get; set; }

        public DbSet<SysCustomer> SysCustomer { get; set; }

        public DbSet<SysCustomerAddress> SysCustomerAddress { get; set; }

        public DbSet<SysCustomerOpenID> SysCustomerOpenID { get; set; }

        public DbSet<SysShareRecord> SysShareRecord { get; set; }

        public DbSet<SysQrCode> SysQrCode { get; set; }

        public DbSet<SysFeedback> SysFeedback { get; set; }

        public DbSet<SysFeedbackRecord> SysFeedbackRecord { get; set; }

        #endregion

        protected override void OnModelCreating(ModelBuilder builder)
        {
            ////移除表名复数 老EF的功能
            //builder.Conventions.Remove<PluralizingTableNameConvention>();

            #region 自定义函数

            builder.HasDbFunction(
                typeof(DbFun).GetMethod(nameof(DbFun.fn_getDistance), BindingFlags.Static | BindingFlags.Public),
                options =>
                {
                    options.HasName(nameof(DbFun.fn_getDistance));
                    options.HasSchema("dbo");
                });

            builder.HasDbFunction(
                typeof(DbFun).GetMethod(nameof(DbFun.fn_replaceNum), BindingFlags.Static | BindingFlags.Public),
                options =>
                {
                    options.HasName(nameof(DbFun.fn_replaceNum));
                    options.HasSchema("dbo");
                });

            //builder.HasDbFunction(
            //    typeof(DbFun).GetMethod(nameof(DbFun.fn_overDay), BindingFlags.Static | BindingFlags.Public),
            //    options =>
            //    {
            //        options.HasName(nameof(DbFun.fn_overDay));
            //        options.HasSchema("dbo");
            //    });

            #endregion

            #region decimal精度

            builder.Entity<SysStore>().Property(p => p.Longitude).HasPrecision(18, 8);
            builder.Entity<SysStore>().Property(p => p.Latitude).HasPrecision(18, 8);
            builder.Entity<SysCustomerAddress>().Property(p => p.Longitude).HasPrecision(18, 8);
            builder.Entity<SysCustomerAddress>().Property(p => p.Latitude).HasPrecision(18, 8);

            #endregion

            this.OnModelCreatingAuto(builder);
            this.OnModelCreatingPartial(builder);
            base.OnModelCreating(builder);
        }
    }
}
